/*
 * GameRabbit.h
 *
 *  Created on: 2011-11-29
 *      Author: baixp
 */

#ifndef GAMERABBIT_H_
#define GAMERABBIT_H_

#include "Lib9.h"
#include "L9Map.h"
#include "L9ResStr.h"
#include "L9Store.h"
#include "GameRabbitLogo.h"

using namespace lib9bada;

// 文字资源,由L9Eidtor工具导出的文本字符串常量,在制作多语言版本非常方便
static int K_TEXT_GAME_RULE = 0;
static int K_TEXT_EXIT_GAME = 1;
static int K_TEXT_HIGH_SCORE = 2;
static int K_TEXT_OVER_LAST_HIGH_SCORE = 3;
static int K_TEXT_GAME_OVER = 4;
static int K_TEXT_GAME_OVER2 = 5;
static int K_TEXT_DIALOG_TITLE = 6;
static int K_TEXT_BUTTON_PAUSE = 7;
static int K_TEXT_BUTTON_BACK = 8;
static int K_TEXT_BUTTON_COMFIRE = 9;
static int K_TEXT_PAUSE_TIP = 10;
static int K_TEXT_GAME_TOP_TITLE = 11;

// L9Editor导出的帧、动画、地图等资源常量
static int K_FRAME_BEAR = 0;
static int K_FRAME_RABBIT = 1;
static int K_FRAME_BACK = 4;
static int K_FRAME_PAUSE = 5;
static int K_ANIM_HAMMER = 0;

static int K_Default_FPS = 15;

static const int K_Minigame_Rabbit_Type_Xiong = 0;
static const int K_Minigame_Rabbit_Type_Rabbit = 1;

//注意存储的文件的目录，请参考bada文件系统
static const char* K_Rabbit_DB = "/Home/rms_config";

static const int K_Soft_Key_W = 105;
static const int K_Soft_Key_H = 50;

static const long K_Minigame_Game_Time = 2 * 60 * 1000;

// //////////////////////////////////////兔子、熊和锤子出现的坐标,参照树桩的坐标///////////////////////
static int K_Minigame_Rabbit_Rabbit_X_Off = 24; // 相对于树桩的偏移量
static int K_Minigame_Rabbit_Rabbit_Y_Off = -66;
static int K_Minigame_Rabbit_Rabbit_X_Off2 = 0; // 锤子相对于兔子的偏移量
static int K_Minigame_Rabbit_Rabbit_Y_Off2 = 0;
class GameRabbit: public Lib9, public L9IState {
private:
	String Str_Left_Soft;
	String Str_Right_Soft;
public:
	//	GameRabbit();
	virtual ~GameRabbit();

	int getRabbitX() {
		int XX = spriteRabbit_Map->getModuleX(Minigame_Rabbit_Index);
		return XX + K_Minigame_Rabbit_Rabbit_X_Off;
	}

	int getRabbitY() {
		int YY = spriteRabbit_Map->getModuleY(Minigame_Rabbit_Index);
		return YY + K_Minigame_Rabbit_Rabbit_Y_Off;
	}

	int getHammerX() {
		return getRabbitX() + K_Minigame_Rabbit_Rabbit_X_Off2;
	}

	int getHammerY() {
		return getRabbitY() + K_Minigame_Rabbit_Rabbit_Y_Off2;
	}

	// 游戏资源对象
	L9Map* spriteRabbit_Map;
	L9Animation* spriteHammer;
	L9Sprite* spriteRabbit;
	L9ResStr* pL9ResStr;

	L9IState* pGameLogo;

	GameRabbit(Application* pApp):Lib9(pApp) {
		// TODO Auto-generated constructor stub
		defaultInit();

		readRecord();

		// L9Sprite spritetest=new L9Sprite("/rabbitMap");
		spriteRabbit = new L9Sprite("/res/rabbit");
//		spriteRabbit->cacheImages(0,-1,0);
		spriteHammer = new L9Animation(spriteRabbit, 0);

		spriteRabbit_Map = new L9Map("/res/rabbitMap", SCR_W, SCR_H);
		//缓存地图中的元素
		spriteRabbit_Map->pMapSprite->cacheImages(0, -1, 0);

		// 创建字符串资源对象,使用XXXX为Token,","为分割符号
		pL9ResStr = new L9ResStr("/res/text");
		pL9ResStr->setTokenAndSep("XXXX", ",");

		drawTimeAndScore();

		pGameLogo = new GameRabbitLogo(this);

		// 先进入Logo
		pushState(this);
		changeState(pGameLogo);

	}

	void defaultInit() {
		bDontRestartMinigame = false;
		Minigame_Rabbit_Index = -1;
		Minigame_Rabbit_Type = 0;
		Minigame_Rabbit_Press_Index = -1;
	}

	long Minigame_Time;
	bool Minigame_IsOver;
	int Minigame_Score;
	bool Minigame_bEnter;
	int Minigame_XiongFail_Count;
	bool bDontRestartMinigame;

	void exitGame() {
		bDontRestartMinigame = true;
		String strTitle = pL9ResStr->getResStr(K_TEXT_DIALOG_TITLE);
		String strMsg = pL9ResStr->getResStr(K_TEXT_EXIT_GAME);
		String strYes = pL9ResStr->getResStr(K_TEXT_BUTTON_COMFIRE);
		String strNo = pL9ResStr->getResStr(K_TEXT_BUTTON_BACK);
		showYesNoDialog(strTitle, strMsg, strYes, strNo, 180);
	}

	// String[][] Minigame_HigeScore_Array;
	void updateGame() {
		Minigame_Time += getTicks();

		if (Minigame_Time > K_Minigame_Game_Time) {
			Minigame_IsOver = true;
		}
		if (Minigame_XiongFail_Count > 5) {
			Minigame_IsOver = true;
		}

		if (Minigame_IsOver) {

			String sLine = "";
			String sReplace = L"";
			if (Minigame_XiongFail_Count > 5) {
				sLine = pL9ResStr->getResStr(K_TEXT_GAME_OVER);
			} else {
				if (Last_Time == 0) { // 第一次
					sReplace.Append(Minigame_Score);
					sReplace.Append(",");
					sReplace.Append(((int) (Minigame_Time / 1000)));
					sReplace.Append(",");
					sReplace.Append(Last_Scores);
					sReplace.Append(Last_Time);
					sLine = pL9ResStr->getResStr(K_TEXT_HIGH_SCORE, sReplace);
				} else {
					// 只要大于上次的记录就应该存储
					double last = ((double) Last_Scores) / ((double) Last_Time);
					double rs = ((double) Minigame_Score)
							/ ((double) Minigame_Time);

					sReplace.Append(Minigame_Score);
					sReplace.Append(",");
					sReplace.Append(((int) (Minigame_Time / 1000)));
					sReplace.Append(",");
					sReplace.Append(Last_Scores);
					sReplace.Append(Last_Time);
					if (rs > last) {
						sLine = pL9ResStr->getResStr(
								K_TEXT_OVER_LAST_HIGH_SCORE, sReplace);
					} else {
						sLine
								= pL9ResStr->getResStr(K_TEXT_GAME_OVER,
										sReplace);
					}
				}
			}

			bDontRestartMinigame = false;

			saveGame();

			showMsgDialog(pL9ResStr->getResStr(K_TEXT_DIALOG_TITLE), sLine,
					pL9ResStr->getResStr(K_TEXT_BUTTON_COMFIRE), 180);
		}
	}

	void drawTimeAndScore() {
		// 将毫秒时间转换为 小时：分：秒 这种形式
		long leftTime = K_Minigame_Game_Time - Minigame_Time;
		long hour = leftTime / 3600000;
		long minute = (leftTime - hour * 3600000) / 60000;
		long second = (leftTime - hour * 3600000 - minute * 60000) / 1000;

		String sTime = L"";
		sTime.Append(Minigame_Score);
		sTime.Append(",");
		sTime.Append(hour);
		sTime.Append(":");
		sTime.Append(minute);
		sTime.Append(":");
		sTime.Append(second);
		String Line = pL9ResStr->getResStr(K_TEXT_GAME_TOP_TITLE, "XXXX",
				sTime, ",");
		pL9Str->drawLine(pFG, Line, 0, 4, SCR_W, L9Str::K_Line_Align_Center);
	}

	int Minigame_Rabbit_Index;
	int Minigame_Rabbit_Type;
	int Minigame_Rabbit_Press_Index;
	int Minigame_Rabbit_Speed;
	int Minigame_Rabbit_Begin_Frame;

	void Init() {
		if (pGameLogo != null) {
			delete pGameLogo;
			pGameLogo = null;
		}
		if(pDialogYesNo!=null&&pDialogYesNo->isYesNo()){//确认退出
			delete pDialogYesNo;
			pDialogYesNo=null;
			quitApp();
		}
		// TODO Auto-generated method stub
		if (!bDontRestartMinigame) {
			Minigame_Time = 0;
			Minigame_IsOver = false;
			Minigame_Score = 0;
			Minigame_XiongFail_Count = 0;
			// K_Minigame_Game_Time = 2 * 60 * 1000; // 小游戏玩耍时间为2分钟
		}
		bDontRestartMinigame = false;

		setSoftKey(pL9ResStr->getResStr(K_TEXT_BUTTON_PAUSE),
				pL9ResStr->getResStr(K_TEXT_BUTTON_BACK));

		Minigame_Rabbit_Begin_Frame = frameCount;
	}

	void Update() {
		// TODO Auto-generated method stub
		Minigame_Rabbit_Press_Index = -1;

		if (!spriteHammer->isPlayOver()) {
			return;
		}

		// 控制兔子或者熊出现的速度
		Minigame_Rabbit_Speed = Minigame_Score / 10;
		if (Minigame_Rabbit_Speed >= K_Default_FPS) {
			Minigame_Rabbit_Speed = K_Default_FPS - 1;
		}

		if ((frameCount - Minigame_Rabbit_Begin_Frame) % (K_Default_FPS
				- Minigame_Rabbit_Speed) == 0) {
			Minigame_Rabbit_Index = L9Util::getDiffRandValue(
					Minigame_Rabbit_Index, 0, 100) % 9;
			Minigame_Rabbit_Type = L9Util::getRandValue(0, 100) % 2;
		}

		if (Minigame_Rabbit_Index == -1) {
			return;
		}

		if (isLeftSoft(getPointerX(), getPointerY())) {
			pauseGame();
		} else if (isRightSoft(getPointerX(), getPointerY())) {
			exitGame();
		} else if (isMiddleSoft(getPointerX(), getPointerY())) {
			gameTip();
		} else {
			if (getPointerX() > 0) {
				int XX = getRabbitX();
				int YY = getRabbitY();

				int w = spriteRabbit->getFrameWidth(Minigame_Rabbit_Type);
				int h = spriteRabbit->getFrameHeight(Minigame_Rabbit_Type);

				if (isInRect(getPointerX(), getPointerY(), L9Rect(XX, XX + w,
						YY, YY + h))) {
					Minigame_Rabbit_Press_Index = Minigame_Rabbit_Index;
				}

			}
		}

		updateGame();

		if (Minigame_Rabbit_Press_Index == Minigame_Rabbit_Index) {
			spriteHammer->playAnim(getHammerX(), getHammerY());
			// System.out.println("动画");

			if (Minigame_Rabbit_Type == K_Minigame_Rabbit_Type_Rabbit) {
				Minigame_Score++;
			} else {
				Minigame_Score--;
				if (Minigame_Score < 0) {
					Minigame_Score = 0;
				}

				Minigame_XiongFail_Count++; // 记录打到熊的次数
			}
			Minigame_Rabbit_Index = -1;
		}
	}

	void Paint() {
		// TODO Auto-generated method stub
		if (spriteRabbit_Map == null) {
			return;
		}
		// 绘制背景和树桩
		spriteRabbit_Map->updateMap(pFG);
		spriteRabbit_Map->updateMapObjects(pFG);

		// 绘制得分和时间
		drawTimeAndScore();

		// 绘制兔子或者熊和锤子
		if (Minigame_Rabbit_Index != -1) {
			spriteRabbit->paintFrame(pFG, Minigame_Rabbit_Type, getRabbitX(),
					getRabbitY());
		}
		spriteHammer->updateAnimation(pFG);

		// 绘制左右软件
		drawSoftKey(Str_Left_Soft, Str_Right_Soft);
	}

	void pauseGame() {
		bDontRestartMinigame = true;
		showMsgDialog(pL9ResStr->getResStr(K_TEXT_DIALOG_TITLE),
				pL9ResStr->getResStr(K_TEXT_PAUSE_TIP), pL9ResStr->getResStr(
						K_TEXT_BUTTON_COMFIRE), 180);
	}

	void gameTip() {
		bDontRestartMinigame = true;
		String str = L"";
		str.Append(Last_Scores);
		str.Append(",");
		str.Append(Last_Time);
		String sMsg = pL9ResStr->getResStr(K_TEXT_HIGH_SCORE, str);

		showMsgDialog(pL9ResStr->getResStr(K_TEXT_DIALOG_TITLE), sMsg,
				pL9ResStr->getResStr(K_TEXT_BUTTON_COMFIRE), 180);
	}

	// ////////////////////////////////////////绘制左右软键//////////////////////////////////////////////////
	int K_Soft_Key_Text_Y;

	void setSoftKey(String sLeft, String sRight) {
		Str_Left_Soft = sLeft;
		Str_Right_Soft = sRight;
	}

	int getSoftKeyIcon(String Key) {
		int keyID = -1;
		if (Key.Equals(pL9ResStr->getResStr(K_TEXT_BUTTON_BACK))) {
			keyID = K_FRAME_BACK;
		} else if (Key.Equals(pL9ResStr->getResStr(K_TEXT_BUTTON_PAUSE))) {
			keyID = K_FRAME_PAUSE;
		}
		return keyID;
	}

	void drawSoftKey(String left, String right) {
		if (left != null) {
			int keyID = getSoftKeyIcon(left);
			if (keyID != -1) {
				spriteRabbit->paintFrame(pFG, keyID, 0, SCR_H
						- spriteRabbit->getFrameHeight(keyID));
			}
			// System.out.println("left="+left);
		}
		if (right != null) {
			int keyID = getSoftKeyIcon(right);
			if (keyID != -1) {
				spriteRabbit->paintFrame(pFG, keyID, SCR_W
						- spriteRabbit->getFrameWidth(keyID), SCR_H
						- spriteRabbit->getFrameHeight(keyID));
			}
			// System.out.println("right="+right);
		}
	}

	bool isLeftSoft(int pX, int pY) {
		if (pX > 0 && pX < K_Soft_Key_W && pY > SCR_H - K_Soft_Key_H && pY
				< SCR_H) {
			return true;
		}
		return false;
	}

	bool isRightSoft(int pX, int pY) {
		if (pX > SCR_W - K_Soft_Key_W && pX < SCR_W && pY > SCR_H
				- K_Soft_Key_H && pY < SCR_H) {
			return true;
		}
		return false;
	}

	bool isMiddleSoft(int pX, int pY) {
		if (pX > K_Soft_Key_W && pX < SCR_W - K_Soft_Key_W && pY > SCR_H
				- K_Soft_Key_H && pY < SCR_H) {
			return true;
		}
		return false;
	}

	// ///////////////////////////////记录数据/////////////////////////////////////////////
	int Last_Scores;
	int Last_Time;

	void readRecord() {
		L9Store db(K_Rabbit_DB, L9Store::K_Store_Mode_Read);
		// 第一次读取可能由于没有保存记录将会读取失败，所以这次进行了异常处理
		if (db.isReady()) {
			Last_Scores = db.readInt();
			Last_Time = db.readInt();
		}
	}

	void writeRecord() {
		L9Store db(K_Rabbit_DB, L9Store::K_Store_Mode_Write);
		db.writeInt(Last_Scores);
		db.writeInt(Last_Time);
		db.Save();
	}

	void saveGame() {
		if (Minigame_Time > 0) {
			if (Last_Time == 0) { // 第一次
				Last_Scores = Minigame_Score;
				Last_Time = (int) (Minigame_Time / 1000);
				writeRecord();
			} else {
				// 只要大于上次的记录就应该存储
				double last = ((double) Last_Scores) / ((double) Last_Time);
				double rs = ((double) Minigame_Score)
						/ ((double) Minigame_Time);
				if (rs > last) {
					Last_Scores = Minigame_Score;
					Last_Time = (int) (Minigame_Time / 1000);
					writeRecord();
				}
			}
		}
	}
};

#endif /* GAMERABBIT_H_ */
